package controllers

import (
	"encoding/json"
	//"errors"
	//"strconv"
	//"strings"
	"gitee.com/lzqysoft/ims/models"

	"github.com/astaxie/beego"
)

// BuyItemController operations for Buys
type BuyItemController struct {
	CommonController
}

// URLMapping ...
func (c *BuyItemController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
}

// Post ...
// @Title Post
// @Description create Buys
// @Param	body		body 	models.Buys	true		"body for Buys content"
// @Success 201 {int} models.Buys
// @Failure 403 body is empty
// @router / [post]
func (c *BuyItemController) Post() {
	var v models.Buys
	beego.Debug("hello")
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		if _, err := models.AddBuys(&v); err == nil {
			c.Ctx.Output.SetStatus(201)
			c.Data["json"] = v
		} else {
			c.Data["json"] = err.Error()
		}
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// GetOne ...
// @Title Get One
// @Description get Buys by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Buys
// @Failure 403 :id is empty
// @router /:id [get]
func (c *BuyItemController) GetOne() {
	idStr := c.Ctx.Input.Param(":id")
	//id, _ := strconv.Atoi(idStr)
	v, err := models.GetBuysById(idStr)
	if err != nil {
		c.Data["json"] = err.Error()
	} else {
		c.Data["json"] = v
	}
	c.ServeJSON()
}

// GetAll ...
// @Title Get All
// @Description get BuyItem
// @Param	id	id	string	true	"Filter. e.g. b58da041-32f0-11e8-82e1-c85b76db6e9c"
// @Success 200 {object} models.Buys
// @Failure 403
// @router /GetAll/:id [get]
func (c *BuyItemController) GetAll() {
	id := c.Ctx.Input.Param(":id")
	beego.Debug(id)
	sales, err := models.GetBuyItemByOrder(id)
	receiv, err := models.GetPayByOrderId(id)
	if err != nil {
		beego.Error(err)
		c.RetError( ErrNoData)
	}
	td := make(map[string]interface{})
	td["sales"] = sales
	td["receiv"] = receiv
	c.RetSuccess(td)
}

// Put ...
// @Title Put
// @Description update the Buys
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.Buys	true		"body for Buys content"
// @Success 200 {object} models.Buys
// @Failure 403 :id is not int
// @router /:id [put]
func (c *BuyItemController) Put() {
	idStr := c.Ctx.Input.Param(":id")
	//id, _ := strconv.Atoi(idStr)
	v := models.Buys{Id: idStr}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		if err := models.UpdateBuysById(&v); err == nil {
			c.Data["json"] = "OK"
		} else {
			c.Data["json"] = err.Error()
		}
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// Delete ...
// @Title Delete
// @Description delete the Buys
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *BuyItemController) Delete() {
	idStr := c.Ctx.Input.Param(":id")
	//id, _ := strconv.Atoi(idStr)
	if err := models.DeleteBuys(idStr); err == nil {
		c.Data["json"] = "OK"
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}
